<?php

class reportActions extends sfActions {

    public function preExecute() {
        date_default_timezone_set(AppTools::TIMEZONE);
    }

    public function executeOutcome(sfWebRequest $request) {
        $dateBegin = $request->getParameter('dateBegin', date('Y-m-d'));
        $dateEnd = $request->getParameter('dateEnd', date('Y-m-d'));
        $storeId = $request->getParameter('storeId', 0);
        $userId = $request->getParameter('userId', 0);
        $categoryId = $request->getParameter('categoryId', 0);
        $productId = $request->getParameter('productId', 0);

        $this->dateBegin = $dateBegin;
        $this->dateEnd = $dateEnd;
        $this->storeId = $storeId;
        $this->stores = StoreTable::getForSelect();
        $this->userId = $userId;
        $this->users = UserTable::getForSelect();
        $this->categoryId = $categoryId;
        $this->categories = CategoryTable::getForSelect();
        $this->productId = $productId;

        $urlParams = array();
        $urlParams[] = "user_id=$userId";
        $urlParams[] = "store_id=$storeId";
        $urlParams[] = "cat_id=$categoryId";
        $urlParams[] = "prod_id=$productId";
        $urlParams[] = "date_begin=$dateBegin";
        $urlParams[] = "date_end=$dateEnd";

        $this->urlTable = $this->generateUrl('report_outcome_data') . '?' . implode('&', $urlParams);
        $this->urlExcel = $this->generateUrl('report_outcome_excel') . '?' . implode('&', $urlParams);
        $this->urlProduct = AppTools::getFullUrl() . '/ajax/product';
        $this->dataSummary = ReportCore::getOutcomeDataSummary($userId, $storeId, $dateBegin, $dateEnd, $categoryId, $productId);
    }

    public function executeOutcomeData(sfWebRequest $request) {
        $start = $request->getParameter('jtStartIndex', 0);
        $length = $request->getParameter('jtPageSize', AppTools::DEFAULT_PER_PAGE);
        $sorting = $request->getParameter('jtSorting', "t.id ASC");
        $userId = $request->getParameter('user_id', 0);
        $storeId = $request->getParameter('store_id', 0);
        $categoryId = $request->getParameter('cat_id', 0);
        $productId = $request->getParameter('prod_id', 0);
        $dateBegin = $request->getParameter('date_begin', date('Y-m-d'));
        $dateEnd = $request->getParameter('date_end', date('Y-m-d'));

        $res = ReportCore::getOutcomeData($userId, $storeId, $dateBegin, $dateEnd, $categoryId, $productId, $sorting, $start, $length);

        $result = array();
        $result['Result'] = "OK";
        $result['Records'] = $res['rows'];
        $result['TotalRecordCount'] = $res['numrows'];

        return $this->renderText(json_encode($result));
    }

    public function executeOutcomeExcel(sfWebRequest $request) {
        $userId = $request->getParameter('user_id', 0);
        $storeId = $request->getParameter('store_id', 0);
        $categoryId = $request->getParameter('cat_id', 0);
        $productId = $request->getParameter('prod_id', 0);
        $dateBegin = $request->getParameter('date_begin', date('Y-m-d'));
        $dateEnd = $request->getParameter('date_end', date('Y-m-d'));

        $res = ReportCore::getOutcomeData($userId, $storeId, $dateBegin, $dateEnd, $categoryId, $productId, 't.id ASC', 0, 0, TRUE);

        $excelArray = array();
        $excelArray[] = array('Зарлага#', 'Огноо', 'Салбар', 'Ажилтан', 'Нийт үнэ', 'Бэлэн', 'Карт', 'Ангилал', 'Бараа', 'Тоо', 'Үнэ', 'Хаашаа', 'Хэнрүү', 'Үүсгэсэн', 'Өөрчилсөн');

        foreach ($res['rows'] as $row) {
            $json = '{' . $row['payments'] . '}';
            $payments = json_decode($json, true);
            $paymentCash = isset($payments[TransactionTable::PAYMENT_METHOD_CASH]) ? $payments[TransactionTable::PAYMENT_METHOD_CASH] : 0;
            $paymentCard = isset($payments[TransactionTable::PAYMENT_METHOD_CARD]) ? $payments[TransactionTable::PAYMENT_METHOD_CARD] : 0;
            unset($payments);
            $excelArray[] = array((int) $row['order_id'],
                $row['order_date'],
                $row['store_name'],
                $row['username'],
                (double) $row['total_price'],
                (double) $paymentCash,
                (double) $paymentCard,
                $row['cat_name'],
                $row['prod_name'],
                (int) $row['qty'],
                (double) $row['price'],
                $row['order_type'],
                $row['ordered_to'],
                $row['order_created_at'],
                $row['order_updated_at']);
        }

        $excel = new SimpleExcel('xml');
        $excel->writer->setData($excelArray); // add some data to the writer
        $excel->writer->saveFile('salesReport' . str_replace('-', '', $dateBegin) . '-' . str_replace('-', '', $dateEnd)); // save the file with specified name (example.xml) 
        die();
    }

}
